package com.sig.load;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.sig.models.Point;
import com.sig.models.Triangle;

public class LoadPolygons extends DefaultHandler implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String BALISE_POLYGONS="polygons";
	public static final String BALISE_POLYGON="polygon";
	public static final String BALISE_TRIANGLE="triangle";
	public static final String BALISE_POINT1="point1";
	public static final String BALISE_POINT2="point2";
	public static final String BALISE_POINT3="point3";
	public static final String ATTRIBUTE_X="x";
	public static final String ATTRIBUTE_Y="y";
	public static final String TAG="TAG";

	
	private List<Triangle> listeTriangle;
	private List<List<Triangle>> listePolygons=new ArrayList<List<Triangle>>();
	private Triangle triangle;
	private Point p1,p2,p3;
	public ArrayList<Point> litepoint;
	
	
	
	public LoadPolygons(File infile) {
		super();
		this.load(infile, this);
		
	}


	/**
	 * Call if begin of tag
	 */
	public void startElement(String uri, String localName,
			String qName, Attributes attributes) throws SAXException{
		if(qName.equals(BALISE_POLYGONS)){
			Log.d(TAG, "polygons");
			
		}
		else if(qName.equalsIgnoreCase(BALISE_POLYGON)){
			Log.d(TAG, "polygon");
			listeTriangle=new ArrayList<Triangle>();
		}
		else if(qName.equals(BALISE_TRIANGLE)){
			Log.d(TAG, "create new Triangle");
			litepoint=new ArrayList<Point>();
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT1)){
			Log.d(TAG, "point");			
			p1=new Point(Float.parseFloat(attributes.getValue(ATTRIBUTE_X)),
						Float.parseFloat(attributes.getValue(ATTRIBUTE_Y)));
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT2)){
			Log.d(TAG, "point");			
			p2=new Point(Float.parseFloat(attributes.getValue(ATTRIBUTE_X)),
						Float.parseFloat(attributes.getValue(ATTRIBUTE_Y)));
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT3)){
			Log.d(TAG, "point");			
			p3=new Point(Float.parseFloat(attributes.getValue(ATTRIBUTE_X)),
						Float.parseFloat(attributes.getValue(ATTRIBUTE_Y)));
		}
	}
	
	
	public void endElement(String uri, String localName, String qName)
			throws SAXException{
		if(qName.equals(BALISE_POLYGONS)){
		}
		else if(qName.equalsIgnoreCase(BALISE_POLYGON)){				
			listePolygons.add(listeTriangle);			//add list triangle in listpolygons		
		}
		else if(qName.equalsIgnoreCase(BALISE_TRIANGLE)){						
			triangle=new Triangle(p1,p2,p3);			//create new triangle and add into list
			listeTriangle.add(triangle);
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT1)){
			
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT2)){
			
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT3)){
			
		}
	}
	
	
	/**
	 * Load the file 
	 */
	public void load(File in,LoadPolygons poly) {
		SAXParserFactory saxFactory=SAXParserFactory.newInstance();
		try {
			Log.d("FILE", ""+in);
			SAXParser parser=saxFactory.newSAXParser();			
			parser.parse(in, poly);
			Log.d("FILE", "Read XML");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("FILE", " NOT found");
			e.printStackTrace();
		}
		
	}
	

	/**
	 * @return the listePolygons
	 */
	public List<List<Triangle>> getListePolygons() {
		return listePolygons;
	}
	
}
